package com.example.demo.demos.web;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/test")
@RestController
public class TestController {

    @RequestMapping("/a")
    public String a() {
        return LocalDateTime.now() + ", a";
    }

    /**
     * excel 导出
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls() {

        List<Course> list = buildList();

        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        // 导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "导出Excel文件名字");
        // 注解对象Class
        mv.addObject(NormalExcelConstants.CLASS, Course.class);
        // 自定义表格参数
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("自定义导出Excel模板内容标题", "自定义Sheet名字"));
        // 导出数据列表
        mv.addObject(NormalExcelConstants.DATA_LIST, list);

        return mv;

    }

    private List<Course> buildList() {

        ArrayList<Course> list = new ArrayList<>();

        Course entity = new Course();
        entity.setId(1);
        entity.setName("梅孝亮");
        list.add(entity);

        Course entity1 = new Course();
        entity1.setId(2);
        entity1.setName("梅孝亮2");
        list.add(entity1);

        Course entity2 = new Course();
        entity2.setId(3);
        entity2.setName("梅孝亮3");
        list.add(entity2);

        return list;

    }

}
